package com.xuyiwei.mayi.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuyiwei on 2019/4/20.
 */
@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisService redisService;

    @RequestMapping("/redisMap")
    public String redisMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("aa","aa");
        map.put("bb","bb");
        stringRedisTemplate.opsForHash().putAll("kMap",map);
        stringRedisTemplate.opsForHash().put("kMap","cc","cc");
        Map<Object, Object> resultMap= stringRedisTemplate.opsForHash().entries("kMap");
        for(Map.Entry<Object,Object> entry:resultMap.entrySet()){
            System.out.println("resultMap:"+resultMap.get(entry.getKey()));

        }
        return "放置成功";
    }

    @RequestMapping("/redisList")
    public String redisList(){
        stringRedisTemplate.opsForList().leftPush("listkey1","11");
        stringRedisTemplate.opsForList().leftPush("listkey1","22");
        stringRedisTemplate.opsForList().leftPop("listkey1");
        List<String> list = stringRedisTemplate.opsForList().range("listkey1",0,-1);
        for(String v:list){
            System.out.println(v);
        }
        return "放置成功";
    }

    @RequestMapping("/punishRedis")
    public String punishRedis(){
        redisService.sendChannelMess("xywTest","这是xywTest发布的消息");
        return "放置成功";
    }
}
